#!/bin/sh
#
# vnc -- login with a local vnc client
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#

TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd)"
. "$TOP_DIR"/tools/docker/config >/dev/null

prepare_login || exit 0

log_print "Get cached data"
get_vars VNC_IP VNC_PWD VNC_PWD_VIEWONLY

VNC_CLIENTS="vinagre xvnc4viewer gvncviewer xtightvncviewer krdc mstsc"

vnc_clients()
{
  for v in $VNC_CLIENTS
  do
    which $v >/dev/null 2>&1
    [ $? -eq 0 ] && echo -n "$v "
  done  
}

verify_vnc()
{
  client=$1
  match=0
  number=0

  echo $client | grep -q '[a-z]'
  if [ $? -eq 0 ]; then
    for l in $INSTALLED_VNC_CLIENTS
    do
      number=`expr $number + 1`

      if [ "$l" = "$client" ]; then
        match=1
        return 0
      fi
    done
  else
    total=`echo "$INSTALLED_VNC_CLIENTS " | tr -d -c ' ' | wc -c`
    if [ $client -le $total ]; then
       number=$client
       return 0
    fi
  fi

  return 1
}

vnc_config()
{
  client=$1

  # List available vnc clients
  echo "INFO: Available VNC Clients:"
  echo
  echo $INSTALLED_VNC_CLIENTS | tr ' ' '\n' | nl -v 1
  echo
  
  # Let user choose one
  read -p 'INFO: Choose the vnc client: ' client

  [ -z "$client" ] && echo "ERR: No client specified." && exit 1

  number=0
  verify_vnc $client
  [ $? -ne 0 ] && echo "ERR: No valid client specified." && exit 1

  VNC_CLIENT=`echo $INSTALLED_VNC_CLIENTS | tr ' ' '\n' | head -$number | tail -1`
  echo
  echo $INSTALLED_VNC_CLIENTS | tr ' ' '\n' | nl -v 1 | grep " $number"
  echo
  
  VNC=$VNC_CLIENT

  set_var VNC
} # vnc_config

show_vnc()
{
  AVAILABLE_VNC_CLIENTS=$INSTALLED_VNC_CLIENTS
  echo $AVAILABLE_VNC_CLIENTS | grep -E -q "$VNC"
  [ $? -ne 0 -a "$VNC" != "list" -a "$VNC" != "none" ] && AVAILABLE_VNC_CLIENTS="$VNC $AVAILABLE_VNC_CLIENTS"

  echo
  echo "         VNC Clients: $AVAILABLE_VNC_CLIENTS"
  echo "          VNC Server: $VNC_IP"
  echo "     Normal Password: $VNC_PWD"
  echo "   Viewonly Password: $VNC_PWD_VIEWONLY"
  echo
}

show_usage()
{
  # Report if no vnc client specified
  info_print "No vnc client found, please configure one with one of the following methods:"
  echo
  echo "1. Specify via: $0 VNC_CLIENT"
  echo "2. Or install any one you prefer and add it to $LAB_VNC"
  echo "3. Or install one of the following:"
  echo
  echo "      $VNC_CLIENTS"
  echo
}

# Get login client in this order: command argument, environment variable and .login_client configuration
# If the first argument is valid lab, use the second one as vnc client argument.
if [ -n "$1" ]; then
  LAB=$1
  [ -n "$LAB" ] && get_labs | tr ' ' '\n' | grep -q ^$LAB$
  if [ $? -eq 0 ]; then
    [ -n "$2" ] && VNC=$2
  else
    VNC=$1
  fi
fi

# Get installed vnc clients
INSTALLED_VNC_CLIENTS=`vnc_clients`

if [ "$VNC" != "list" ]; then
  if [ -n "$VNC" ]; then
    which $VNC >/dev/null 2>&1
    [ $? -ne 0 ] && echo "ERR: client $VNC is not installed." && exit 1
    set_var VNC
  else
    get_var VNC none
  fi
fi

# show vnc info
echo
info_print "VNC login information:"
show_vnc

# show usage
if [ "$VNC" = "none" -a -z "$INSTALLED_VNC_CLIENTS" ]; then 
  show_usage
  exit 1
fi

# If no vnc client specified in command line, let use choose always
[ "$VNC" = "none" -o "$VNC" = "list" ] && vnc_config && set_var VNC

case $VNC in
  remmina)
      VNC_LOGIN="$VNC $VNC_IP"
      ;;
  vinagre)
      VNC_LOGIN="$VNC --vnc-scale $VNC_IP"
      ;;
  mstsc)
      VNC_LOGIN="$VNC //v:$VNC_IP"
      ;;
  *)
      VNC_LOGIN="$VNC $VNC_IP"
      ;;
esac

# Easier login
copy2clipboard ()
{
  [ -z "$1" ] && return 1

  clip=""
  for c in xsel xclip clip
  do
    which $c >/dev/null 2>&1 && clip=$c && break
  done

  case "$clip" in
    xsel)  clip_cmd="$clip -b" ;;
    xclip) clip_cmd="$clip -selection clipboard" ;;
    clip)  clip_cmd="$clip" ;;
    *)     err_print "No clipboard command found" && return 1 ;;
  esac

  echo "$1" | tr -d '\r\n' | eval "$clip_cmd"
}

copy2clipboard "$VNC_PWD"

info_print "Running '$VNC_LOGIN'"
echo

nohup $VNC_LOGIN >/dev/null 2>&1 &
